﻿Partial Public Class MoveToMoviePropertiesControl

    Private Sub ButtonStartingAt_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If SelectedMovie Is Nothing Then
            MsgBox("First select a movie")
            Return
        End If
        Dim l_SetMovieStartTimeDialog As New SetMovieStartTimeDialog(SelectedMovie.Path)

        Dim l_window = getTopWindow()
        '   l_SetMovieStartTimeDialog.Owner = Me.Parent
        l_SetMovieStartTimeDialog.Owner = l_window
        If l_SetMovieStartTimeDialog.ShowDialog Then
            ButtonStartingAt.Content = l_SetMovieStartTimeDialog.StartingAt
            CType(Me.DataContext, MoveToMovieResultProperties).StartingAt = l_SetMovieStartTimeDialog.StartingAt
        End If

    End Sub

    Public Sub New(ByVal p_AvailableMovies As List(Of Movie))

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ComboMovieIds.ItemsSource = p_AvailableMovies
        ComboMovieIds.DisplayMemberPath = "ID"


    End Sub

    

    '    public static T TryFindParent<T>(DependencyObject child)
    '        where(T) : DependencyObject()
    '{
    '  //get parent item
    '  DependencyObject parentObject = GetParentObject(child);

    '  //we've reached the end of the tree
    '  if (parentObject == null) return null;

    '  //check if the parent matches the type we're looking for
    '  T parent = parentObject as T;
    '  if (parent != null)
    '  {
    '    return parent;
    '  }
    '            Else
    '  {
    '    //use recursion to proceed with next level
    '    return TryFindParent<T>(parentObject);
    '  }
    '}

    Private Function getTopWindow() As Window
        Dim l_Object As FrameworkElement = Me
        While l_Object IsNot Nothing
            If (TypeOf l_Object Is Window) Then
                Exit While

            End If
            If l_Object.Parent Is Nothing OrElse Not TypeOf l_Object.Parent Is FrameworkElement Then
                Exit While
            End If

            l_Object = CType(l_Object.Parent, FrameworkElement)

        End While
        If l_Object IsNot Nothing AndAlso TypeOf l_Object Is Window Then
            Return CType(l_Object, Window)
        Else
            Return Nothing
        End If

    End Function
    


#Region "Properties"
    Public ReadOnly Property SelectedMovie() As Movie
        Get
            If ComboMovieIds.SelectedItem IsNot Nothing Then
                Return CType(ComboMovieIds.SelectedItem, Movie)
            Else
                Return Nothing
            End If
        End Get
    End Property
#End Region

    Private Sub ComboMovieIds_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        
    End Sub

    Private Sub ButtonStartingAt_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
End Class
